/**
 * 数据加载模块
 */
import { state } from './state.js';
import { getTagList } from './api.js';
import { getNurseList } from './api.js';
import { renderLevel1, renderLevel2, renderLevel3, setLoading, updateContentArea } from './ui.js';

/**
 * 初始化根级数据
 */
export const initData = async () => {
  try {
    setLoading(true);
    console.log('获取根级数据...');
    
    const list = await getTagList();
    console.log('根级数据:', list);
    
    if (list && list.length > 0) {
      state.level1List = list;
      state.activeLevel1 = 0;
      renderLevel1();
      
      // 自动加载第二级数据
      await loadLevel2Data(0);
    } else {
      console.warn('没有获取到根级数据');
      state.level1List = [];
      state.level2List = [];
      state.level3List = [];
      renderLevel1();
      renderLevel2();
      renderLevel3();
    }
  } catch (error) {
    console.error('获取根级数据失败:', error);
    state.level1List = [];
    state.level2List = [];
    state.level3List = [];
    renderLevel1();
    renderLevel2();
    renderLevel3();
  } finally {
    setLoading(false);
  }
};

/**
 * 加载第二级数据
 */
export const loadLevel2Data = async (level1Index) => {
  if (level1Index === -1 || !state.level1List[level1Index]) {
    state.level2List = [];
    state.level3List = [];
    state.activeLevel2 = -1;
    state.activeLevel3 = -1;
    state.currentLevel3Item = null;
    state.nurseList = [];
    renderLevel2();
    renderLevel3();
    updateContentArea();
    return;
  }
  
  const currentItem = state.level1List[level1Index];
  
  // 重置下级数据
  state.level2List = [];
  state.level3List = [];
  state.activeLevel2 = -1;
  state.activeLevel3 = -1;
  state.currentLevel3Item = null;
  state.nurseList = [];
  renderLevel2();
  renderLevel3();
  updateContentArea();
  
  try {
    setLoading(true);
    const pid = currentItem.id;
    console.log('获取第二级数据, pid:', pid);
    
    const list = await getTagList(pid);
    console.log('第二级数据:', list);
    
    // 关闭目录加载状态
    setLoading(false);
    
    if (list && list.length > 0) {
      state.level2List = list;
      state.activeLevel2 = 0;
      renderLevel2();
      
      // 自动加载第三级数据
      await loadLevel3Data(0);
    } else {
      state.level2List = [];
      renderLevel2();
      updateContentArea();
    }
  } catch (error) {
    console.error('获取第二级数据失败:', error);
    state.level2List = [];
    renderLevel2();
    setLoading(false);
    updateContentArea();
  }
};

/**
 * 加载第三级数据
 */
export const loadLevel3Data = async (level2Index) => {
  if (level2Index === -1 || !state.level2List[level2Index]) {
    state.level3List = [];
    state.activeLevel3 = -1;
    state.currentLevel3Item = null;
    state.nurseList = [];
    renderLevel3();
    updateContentArea();
    return;
  }
  
  const currentItem = state.level2List[level2Index];
  
  // 重置下级数据
  state.level3List = [];
  state.activeLevel3 = -1;
  state.currentLevel3Item = null;
  renderLevel3();
  updateContentArea();
  
  try {
    setLoading(true);
    const pid = currentItem.id;
    console.log('获取第三级数据, pid:', pid);
    
    const list = await getTagList(pid);
    console.log('第三级数据:', list);
    
    if (list && list.length > 0) {
      state.level3List = list;
      state.activeLevel3 = 0;
      
      // 自动选择第一个并触发变更
      const firstItem = state.level3List[0];
      state.currentLevel3Item = firstItem;
      renderLevel3();
      
      // 先关闭目录加载状态，然后加载护士列表
      setLoading(false);
      
      // 加载护士列表
      if (firstItem && firstItem.id) {
        await getNurseList(firstItem.id);
      } else {
        updateContentArea();
      }
      console.log('已自动选择第三级第一个项目');
    } else {
      state.level3List = [];
      renderLevel3();
      setLoading(false);
      updateContentArea();
    }
  } catch (error) {
    console.error('获取第三级数据失败:', error);
    state.level3List = [];
    renderLevel3();
    setLoading(false);
    updateContentArea();
  }
};

